package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.entity.Factories;
import com.dc.business.datacollectbusiness.entity.WorkShop;
import com.dc.business.datacollectbusiness.mapper.FactoriesMapper;
import com.dc.business.datacollectbusiness.mapper.WorkShopMapper;
import com.dc.business.datacollectbusiness.service.WorkShopService;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkShopServiceImpl implements WorkShopService {

    @Autowired
    private WorkShopMapper workShopMapper;

    @Autowired
    private FactoriesMapper factoriesMapper;

    @Override
    public List<WorkShop> getAllWorkShopsByFactoryId(Integer factoryId) {
        return workShopMapper.selectAllWorkShopByFactoryId(factoryId);
    }

    @Override
    public boolean deleteWorkShopsById(int workShopId) {
        return workShopMapper.deleteWorkShopsById(workShopId);
    }

    @Override
    public boolean insertWorkShops(WorkShop workShop) {

        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        workShop.setCreateBy(map.get("username").toString());

        Factories factories=new Factories();
        factories.setFactoryId(workShop.getFactoryId());
        factoriesMapper.updateFactories2(factories);

        return workShopMapper.insertWorkShops(workShop);
    }

    @Override
    public boolean updateWorkShops(WorkShop workShop) {

        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        workShop.setUpdateBy(map.get("username").toString());

        return workShopMapper.updateWorkShops(workShop);
    }
}
